<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script type="text/javascript">
        /**
        * @param {number[][]} envelopes
        * @return {number}
        */
        function compare(a,b)
        {
            if(a[0]==b[0])
            return b[1]-a[1];
            return a[0]-b[0];
        }
        var maxEnvelopes = function(ans) {
            ans.sort(compare);
            var st=[];
            for(var i=0;i<ans.length;i++)
            {
                var l=0;
                var r=st.length;
                while(l<r)
                {
                    var mid=Math.floor((l+r)/2);
                    if(st[mid]<ans[i][1])
                    {
                        flag=l;
                        l=mid+1;
                    }
                    else{
                        r=mid-1;
                    }
                }
                if(l==st.length)
                {
                    st.push(ans[i][1]);
                }else{
                    st[l]=ans[i][1];
                }
                // console.log(st);
            }
            return st.length;
            // console.log(st.length);
        };
        var envelopes = [[2,100],[4,300],[5,500],[5,400],[5,250],[6,370],[6,360],[7,380]];
        //
        maxEnvelopes(envelopes);

    </script>
</body>
</html>